package com.chapp.lastFmWs.parser;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.sax.Element;
import android.sax.ElementListener;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.StartElementListener;
import android.sax.TextElementListener;
import android.util.Log;

import com.chapp.base.Event;
import com.chapp.base.Location;
import com.chapp.base.Venue;
import com.chapp.lastFmWs.parser.base.ResponseParser;

public class EventSearchParser extends ResponseParser {
	private final String tag = getClass().getName();

	private Element elementEvents = rootLmf.getChild("events");
	private Element elementEvent = elementEvents.getChild("event");

	private final Event event = new Event();
	private final Location location = new Location();
	private final Venue venue = new Venue();

	private SearchResponse<Event> response = new SearchResponse<Event>();

	public EventSearchParser() {
		// Log.d(tag, "construct1");
		// <events artist="Cher" page="1" perPage="50" totalPages="1" total="7"
		// xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
		elementEvents.setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				createListeners();
				response.setQuery(attributes.getValue(0));
				response.setStatusOk(statusOk);
				if (attributes.getValue("total") != null) {
					response.setTotalResults(Integer.parseInt(attributes
							.getValue("total")));
				}
			}
		});
	}

	private void createListeners() {

		elementEvent.setElementListener(new ElementListener() {
			@Override
			public void end() {
				event.setVenue(venue.copy());
				Log.e(tag, venue.toString());
				response.addSearchedItem(event.copy());
			}

			@Override
			public void start(Attributes attributes) {
				event.clean();
				location.clean();
				venue.clean();
			}
		});

		// <image
		// size="medium">http://userserve-ak.last.fm/serve/64/49293069.jpg</image>
		elementEvent.getChild("image").setTextElementListener(
				new TextElementListener() {
					private String size;

					@Override
					public void end(String body) {
						if (size.equals("large") && body != null
								&& !body.equals("")) {
							event.setThumbnailURL(body);
						}
					}

					@Override
					public void start(Attributes attributes) {
						size = attributes.getValue(0);
					}
				});

		// <id>1780310</id>
		elementEvent.getChild("id").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						event.setId(body);
					}
				});

		// <title>Kristin Hersh: Paradoxical Undressing</title>
		elementEvent.getChild("title").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						event.setTitle(body);
					}
				});

		// <artists>
		// <artist>Kristin Hersh</artist>
		// <headliner>Kristin Hersh</headliner> no lo tomamos en cuenta por
		// ahora
		// </artists>
		elementEvent.getChild("artists").getChild("artist")
				.setEndTextElementListener(new EndTextElementListener() {
					@Override
					public void end(String body) {
						event.addArtist(body);
					}
				});

		// <startDate>Wed, 26 Jan 2011 19:30:00</startDate>
		elementEvent.getChild("startDate").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						event.setDate(body);
					}
				});

		// <description/>
		elementEvent.getChild("description").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						event.setDescription(body);
					}
				});

		// <website>http://www.arnolfini.org.uk/whatson/events/details/918</website>
		elementEvent.getChild("website").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						event.setTicketsInfo(body);
					}
				});

		// <cancelled>0</cancelled>
		elementEvent.getChild("cancelled").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						if (body.equals("1"))
							event.setCancelled(true);
					}
				});

		// <venue>
		elementEvent.getChild("venue").setEndElementListener(
				new EndElementListener() {
					@Override
					public void end() {
						venue.setLocation(location.copy());
					}
				});

		UtilParser.venueListener(elementEvent.getChild("venue"), venue,
				location);

	}

	public SearchResponse<Event> parse(String xml) {
		try {
			parseXml(xml);
		} catch (SAXException e) {
			Log.e(xml, "Error Parsing XML", e);
		}

		if (!statusOk) {
			response.setError(error);
		}

		return response;
	}
}
